package dao;

import domain.Permission;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface PermissionDao {

    @Select("SELECT * FROM permission WHERE id IN(SELECT permissionId FROM role_permission WHERE roleId=#{roleId})")
    List<Permission> findByRoleId(int roleId);


    //查询所有
    @Select("select * from permission")
    List<Permission> findAll();


    //新增
    @Insert("insert into permission values(null,#{permissionName},#{permissionDesc})")
    void add(Permission permission);

    //根据id查询
    @Select("select * from permission where id=#{id}")
    @Results({
            @Result(id = true,property = "id",column = "id"),
            @Result(property = "roles",column = "id",many = @Many(select = "dao.RoleDao.findByPermissionId"))
    })
    Permission findById(int id);

    //修改
    @Update("update permission set permissionName=#{permissionName},permissionDesc=#{permissionDesc} where id=#{id}")
    void update(Permission permission);
}
